package netssl;

import javax.net.ssl.SSLSocketFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class J_SSLClient {
    public static void main(String[] args){
        String host = "192.168.5.100";
        int port = 5000;
        //指定信任密钥库
        System.setProperty("javax.net.ssl.trustStore","F:\\temp2\\client.trustStore");
        //获取套接字工厂的实例对象
        SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();

        try {
            Socket s = sf.createSocket(host,port);
            InetAddress sa = s.getInetAddress();
            InetAddress ca = s.getLocalAddress();

            System.out.println("客户端接收服务器端信息：");
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String str = br.readLine();
            System.out.println(str);

            System.out.println("客户端向服务器端发送信息：");
            PrintWriter pw = new PrintWriter(s.getOutputStream());
            str = "客户端"+ca.getHostAddress()+"("+s.getLocalPort()+")"+"应答服务器端"+sa.getHostAddress()+"("+s.getPort()+"）。";
            System.out.println(str);
            pw.println(str);
            pw.flush();

            pw.close();
            br.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
